package com.tian.excel.controller;

import com.tian.excel.service.UserService;
import com.tian.excel.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Tian
 * @version: 1.0
 * @since: 1.0
 * @date: 2024/7/27/11:36
 */
@RestController
@RequestMapping("user")
@Api("查询用户信息")
public class UserController {

    @Autowired
    UserService userServiceImpl;


    @GetMapping
    @ApiOperation("查看登录者信息")
    public  Authentication userInfo(){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        UserDetails principal = (UserDetails)authentication.getPrincipal();
        System.out.println(principal.getUsername());
        return authentication;
    }

    @GetMapping("/selectAll")
    @ApiOperation("查询所有用户信息")
    @PreAuthorize("hasAuthority('user:query')")
    public R selectAll(){
        System.out.println("查询所有用户信息");
        return userServiceImpl.selectAll();
    }

    @DeleteMapping("/deleteById")
    @PreAuthorize("hasAuthority('user:delete')")
    @ApiOperation("删除用户信息")
    public R deleteById(){
        System.out.println("删除用户信息");
        return new R(200,"删除成功",null);
    }

    @PutMapping("/updateUser")
    @PreAuthorize("hasAuthority('user:update')")
    @ApiOperation("修改用户信息")
    public R updateUser(){
        System.out.println("修改用户信息");
        return new R(200,"修改成功",null);
    }


    @PostMapping("/insertUser")
    @PreAuthorize("hasAuthority('user:add')")
    @ApiOperation("添加用户信息")
    public R insertUser(){
        System.out.println("添加用户信息");
        return new R(200,"添加成功",null);
    }

    @GetMapping("/export")
    @PreAuthorize("hasAuthority('user:export')")
    @ApiOperation("导出用户信息")
    public R export(){
        System.out.println("导出用户信息");
        return new R(200,"导出成功",null);
    }


}
